package com.quanyan.authority.service.impl;


import com.lefu.commons.utils.Page;
import com.quanyan.authority.cache.AuthorityRedisTemplate;
import com.quanyan.authority.constants.AuthorityConstants;
import com.quanyan.authority.mapper.FunctionMapper;
import com.quanyan.authority.mapper.TbFunctionMapper;
import com.quanyan.authority.model.db.TbAdminUserRole;
import com.quanyan.authority.model.db.TbFunction;
import com.quanyan.authority.model.request.ReqFunction;
import com.quanyan.authority.model.request.ReqPage;
import com.quanyan.authority.service.FunctionService;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * Created by jian.zhou on 2016/7/28.
 */
@Service
public class FunctionServiceImpl implements FunctionService{

    private static final Logger logger = LoggerFactory.getLogger(FunctionServiceImpl.class);

    @Autowired
    private TbFunctionMapper tbFunctionMapper;

    @Autowired
    private FunctionMapper functionMapper;

    @Autowired
    private AuthorityRedisTemplate authorityRedisTemplate;

    @Override
    public TbFunction findById(Integer id) {
        if(id == null)
           return null;
        return tbFunctionMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbFunction add(ReqFunction reqBaseParam) {
        TbFunction tbFunction = new TbFunction();
        tbFunction.setName(reqBaseParam.getName());
        tbFunction.setCode(reqBaseParam.getCode());
        tbFunction.setRemark(reqBaseParam.getRemark());
        tbFunction.setFounder(reqBaseParam.getFounder());
        tbFunction.setUrl(reqBaseParam.getUrl());
        tbFunction.setCreateTime(new Date());
        tbFunction.setUpdateTime(new Date());
        tbFunction.setIsDelete(Constants.IS_DELETE_FALSE);
        tbFunctionMapper.insert(tbFunction);
        return tbFunction;
    }

    @Override
    public void del(Integer id) {
        TbFunction tbFunction = new TbFunction();
        tbFunction.setId(id);
        tbFunction.setIsDelete(Constants.IS_DELETE_TRUE);
        tbFunctionMapper.updateByPrimaryKeySelective(tbFunction);
    }

    @Override
    public void update(ReqFunction reqFunction) {
        TbFunction tbFunction = new TbFunction();
        tbFunction.setId(reqFunction.getId());
        tbFunction.setName(reqFunction.getName());
        tbFunction.setCode(reqFunction.getCode());
        tbFunction.setRemark(reqFunction.getRemark());
        tbFunction.setUrl(reqFunction.getUrl());
        tbFunctionMapper.updateByPrimaryKeySelective(tbFunction);
    }

    @Override
    public Page findAll(ReqPage reqPage,ReqFunction reqFunction) {
        Page page = new Page();
        page.setCurrentPage(reqPage.getPageNum());
        page.setShowCount(reqPage.getPageSize());
        List<TbFunction> lst = functionMapper.findAll(page, reqFunction.toMap());
        page.setObject(lst);
        logger.info("{}",page.toString());
        return page;
    }


    @Override
    public List<TbFunction> getUserFunction(Integer uid) {
        List<TbAdminUserRole> roleIdList = (List<TbAdminUserRole>)authorityRedisTemplate.get(AuthorityConstants.getUserRoleRedisKey(uid)); //用户拥有的角色
        List<Integer> lst = new ArrayList<>();
        for (TbAdminUserRole ur :roleIdList){
            lst.add(ur.getRoleId());
        }
        if(lst != null){
            List<TbFunction> lstFunction = functionMapper.getUserFunction(lst);
            this.removeDuplicate(lstFunction);
            return lstFunction;
        }
        return null;
    }

    public   void  removeDuplicate(List list)   {
        HashSet h  =   new  HashSet(list);
        list.clear();
        list.addAll(h);
    }
}
